<?php

namespace app\models;

use Yii;
use yii\base\ErrorException;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\CategorySub;
use app\models\CategorySubSub;

/**
 * This is the model class for table "category".
 *
 * @property integer $id
 * @property string $title
 * @property string $slug
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'slug'], 'required'],
            [['title', 'slug'], 'string', 'max' => 255],
            [['slug'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'slug' => 'Slug',
        ];
    }

    /********************** 自定义方法 **********************************/

    /**
     * 该方法顾名思义
     * @param $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();


        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

//        if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
//            return $dataProvider;
//        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'slug', $this->slug]);


        return $dataProvider;
    }


    public static function getCategoryByLevel( $level = null ){

        $get = Yii::$app->request->get();
        if( is_null($level) ){
            $level = isset( $get['level'] ) ? $get['level'] : 0;
        }

        if( $level == 0 ){
            return new self();
        }else if( $level == 1 ){
            return new CategorySub();
        }else if( $level == 2 ){
            return new CategorySubSub();
        }else{
            throw new ErrorException('The level value must be in (0,1,2)');
        }

    }

    public static function getChildCategory( $parent_id, $level ){

        $filter = array();
        if( $level == 0 ){

        }else if( $level == 1 ){
            $filter['category_id'] = $parent_id;
        }else if( $level == 2 ){
            $filter['category_sub_id'] = $parent_id;
        }else{
            throw new ErrorException('the parent level value must be in (0,1,2)');
        }

        $model = self::getCategoryByLevel( $level );

        $categories = $model->find()->where( $filter )->all();


//        print_r( $model );
        $catArr = array();
        foreach( $categories as $cat ){
            $item = array(
                'id'=> $cat->id,
                'title' => $cat->title,
                'slug'  => $cat->slug,
            );
            if( $keys = array_keys( $filter ) ){
                $item[ $keys[0] ] = $cat->$keys[0];
            }
            $catArr[] = $item;
        }
//        print_r( $categories[0]->title );
//        die();
        return $catArr;
    }
}
